"""工程公用的协议解析模块"""
"""
    囊括了11b,11bs,15,15s项目的协议解析器
    如果后续有需要添加的项目，即可直接使用或者修改该模块即可
"""
from screen.JsonProtocolParser.Json_Analysis import JsonProtocol


class PublicDataProcess:

    def __init__(self,json_protocol):
        """
        初始化
        """
        self.__json_protocol = json_protocol
        self.__start_flag = 1
        self.__trans_flag = 2
        self.__end_flag = 3
        self.__erro_flag = 'ff'

    def __start_data_process(self, info: bytes) -> int:
        """
        对起始帧进行解析
        :param info:
        :return:返回实际获取到的数据长度(整型)
        """
        return int.from_bytes(info[2:3], byteorder='big')

    def __trans_data_process(self, info: bytes) -> list:
        """
        对传输帧进行解析
        :param info:解析的数据
        :return:返回列表类型的数据
        """
        # 获取板卡信息
        board_info = int.from_bytes(info[1:2], byteorder='big')

        # 获取主从板卡信息
        board_master_slave = int.from_bytes(info[2:3], byteorder='big')

        # 获取组号
        board_num = int.from_bytes(info[3:4], byteorder='big')

        # 获取当前测试高电平的序号
        board_high_idx = int.from_bytes(info[4:5], byteorder='big')

        # 获取测试到的高电平信号
        board_high_signal = int.from_bytes(info[5:6], byteorder='big')

        # 判断主从板卡信息：0x01：1~128；0x02：128~256；0x03：256~384；
        if board_master_slave == 1:
            board_high_idx = board_high_idx
            board_high_signal = board_high_signal
        elif board_master_slave == 2:
            board_high_idx = board_high_idx + 128
            if board_high_signal == 0:
                board_high_signal = 0
            else:
                board_high_signal = board_high_signal + 128
        elif board_master_slave == 3:
            board_high_idx = board_high_idx + 256
            if board_high_signal == 0:
                board_high_signal = 0
            else:
                board_high_signal = board_high_signal + 256

        return [board_info, board_master_slave, board_num, board_high_idx, board_high_signal]

    def public_data_process(self, info: bytes) -> list:
        """
        公共数据协议处理函数
        :param info:消息体的数据
        :return: 返回列表类型的数据:功能码+数据
        """
        # 获取功能码
        func_code = int.from_bytes(info[0:1], byteorder='big')

        # 如果是开始帧，就返回数据长度
        if func_code == self.__start_flag:
            return [func_code, self.__start_data_process(info)]

        # 如果是传输帧，就返回列表类型的数据
        elif func_code == self.__trans_flag:
            return [func_code] + self.__trans_data_process(info)

        # 如果是结束帧，就返回空列表
        elif func_code == self.__end_flag:
            return [func_code]

        # 如果是错误帧
        elif func_code == int(self.__erro_flag, 16):
            return [func_code]

        else:
            return []

    def master_nck_slave(self)->bytes:
        """
        打包字节传输过程中的响应帧
        :return:待发送的字节串
        """
        # 构造消息体的数据
        try:
            msg_data = (bytes.fromhex('0' + self.__json_protocol.dataprotocol) + int.to_bytes(self.__trans_flag, 1, byteorder='big') + b'\x00\x00')
            return self.__json_protocol.json_pack(msg_data)
        except Exception as e:
            print(e)


